package com.example.blog.service;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.entity.Blog;
import com.example.blog.entity.Result;
import com.example.blog.mapper.BlogMapper;
import com.example.blog.utils.UserHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class BlogService extends ServiceImpl<BlogMapper, Blog> {

    //private updateBlog()
    public synchronized Result insertBlog(Blog blog){
        final String title = blog.getTitle();

        if(title == null || title.isEmpty()){
            return Result.fail("标题不能为空");
        }
        if(blog.getClassify() == null || blog.getClassify().isEmpty()){
            return Result.fail("分类不能为空");
        }
        if(blog.getContent() == null){
            blog.setContent("");
        }

        // TODO 并发访问 标题会重复插入
        final List<Blog> blogList = query()
                .eq("title", title)
                .eq("user_id", UserHolder.getUser().getUserId())
                .list();

        blog.setLastModifyDate(LocalDateTime.now());
        blog.setUserId(UserHolder.getUser().getUserId());

        if(!blogList.isEmpty()){
            blog.setBlogId(blogList.get(0).getBlogId());
            return Result.ok("保存成功", baseMapper.updateById(blog));
        }

        return Result.ok("上传成功", baseMapper.insert(blog));
    }


    public Result getBlogByTitle(String title) {

        final Blog blog = query()
                .eq("user_id", UserHolder.getUser().getUserId())
                .eq("title", title)
                .one();

        if(blog == null){
            return Result.ok(null);
        }

        return Result.ok(blog);
    }

    public Result getTitleByClassify(String Classify) {

        final QueryChainWrapper<Blog> blogs = query()
                .eq("user_id", UserHolder.getUser().getUserId())
                .apply("FIND_IN_SET({0}, Classify) > 0", Classify);

        List<String> list = new ArrayList<>();
        for (Blog blog : blogs.list()) {
            list.add(blog.getTitle());
        }

        return Result.ok(list);
    }

    public Result getClassify() {
        Set<String> classify = new HashSet<>();
        final List<Blog> blogs = query()
                .eq("user_id", UserHolder.getUser().getUserId())
                .list();
        for (Blog blog : blogs) {
            Collections.addAll(classify, blog.getClassify().split(","));
        }
        return Result.ok(classify);
    }

    public Result delBlog(String title) {
        final List<Blog> blogs = query().eq("user_id", UserHolder.getUser().getUserId())
                .eq("title", title).list();
        if(blogs.isEmpty()){
            return Result.fail(title + ": 删除失败");
        }
        final int delRes = baseMapper.deleteById(blogs.get(0).getBlogId());
        return Result.ok(title + ": 删除成功");

    }

}
